package Translate;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.StringTokenizer;


/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2021-12-06
 * Time:16:34
 */
public class Translate extends JFrame implements ActionListener {

    Box baseBox, box1, box2, box3, box4;
    JTextArea input, translate1, translate2, translateCompare;

    public Translate(){
        this.setTitle("中译英翻译窗口");
        baseBox = Box.createVerticalBox();
        box1 = Box.createHorizontalBox();
        box1.add(new JLabel("中文："));
        box1.add(Box.createVerticalStrut(10));

        input = new JTextArea("请在这里输入要翻译的中文",4,30);
        input.setLineWrap(true);
        input.add(new JScrollPane());
        box1.add(input);
        JButton button = new JButton("翻译");
        box1.add(button);
        button.addActionListener(this);
        baseBox.add(box1);
        baseBox.add(Box.createVerticalStrut(20));

        box2 = Box.createHorizontalBox();
        box2.add(new JLabel("百度翻译："));
        box2.add(Box.createVerticalStrut(10));

        translate1 = new JTextArea(4,30);
        translate1.setLineWrap(true);
        translate1.setWrapStyleWord(true);
        box2.add(translate1);
        baseBox.add(box2);
        baseBox.add(Box.createVerticalStrut(20));

        box3 = Box.createHorizontalBox();
        box3.add(new JLabel("讯飞翻译："));
        box3.add(Box.createVerticalStrut(10));

        translate2 = new JTextArea(4,30);
        translate2.setLineWrap(true);
        translate2.setWrapStyleWord(true);
        box3.add(translate2);
        baseBox.add(box3);
        baseBox.add(Box.createVerticalStrut(20));

        box4 = Box.createHorizontalBox();
        box4.add(new JLabel("翻译比较："));
        box4.add(Box.createVerticalStrut(10));

        translateCompare = new JTextArea(4,30);
        translateCompare.setLineWrap(true);
        translateCompare.setWrapStyleWord(true);
        box4.add(translateCompare);
        baseBox.add(box4);

        FlowLayout flowLayout = new FlowLayout();
        flowLayout.setAlignment(FlowLayout.LEFT);
        setLayout(flowLayout);

        add(baseBox);
        validate();
        setBounds(100, 100, 500, 500);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String str = input.getText();
        String ret = Main.trans(str);
        String tmp = "";
        StringTokenizer token = new StringTokenizer(ret,"\"}]}");
        while(token.hasMoreTokens()){
            tmp = token.nextToken();
        }
        translate1.setText(tmp);
    }

    public static void main(String[] args) {
        Translate t = new Translate();
    }
}
